const fs = require("fs");

const { themeList, getBlockSchema, writeFile } = require("../tool/util");

function getStylesheet(src) {
    try {
        const content = fs.readFileSync(src, "utf-8");
        const schemaRegExpStr = /{% stylesheet %}([\s\S]*?){% endstylesheet %}/g;
        const schemBody = content.match(schemaRegExpStr);
        if (schemBody) {
            return schemBody[0].replace(/({% stylesheet %})|({% endstylesheet %})/g, "");
        }
        return "";
    } catch (err) {
        console.log("获取style失败", src);
    }
}

function main() {
    let blocks = "";
    const list = themeList();
    list.forEach((template) => {
        if (template === "Stockholm") {
            console.log(template);
            const sections = fs.readdirSync(`./src/theme/${template}/sections`);
            sections.forEach((section) => {
                const scss = getStylesheet(`./src/theme/${template}/sections/${section}`);
                if(scss){
                  blocks+=`@import "@/modules/${section.replace('.liquid','')}.scss"; \n`
                  writeFile(`./src/theme/${template}/lib/style/modules/${section.replace('.liquid','')}.scss`, scss);
                }
            });
        }
        // if (!fs.existsSync(`./src/theme/${template}/lib/style`)) {
        //     if (template === "Stockholm") {
        //         console.log(template);

        //         const sections = fs.readdirSync(`./src/theme/${template}/sections`);
        //         sections.forEach((section) => {
        //             const scss = getStylesheet(`./src/theme/${template}/sections/${section}`);
        //             writeFile(`./src/theme/${template}/lib/style/modules/${section}.scss`, scss);
        //         });
        //     }
        // }
    });
    console.log(blocks);
}

main();
